<!DOCTYPE html>
<html>
  <head>
		<style type="text/css">
		.cancel_btn
		{
		margin:10px 0px 5px 35px
		}
		.continue_btn
		{
		margin:10px 35px 5px 0px
		}
		.selection_btn
		{
		background-image: url("file:///android_res/drawable/dropdown_bg.png");
		}
		</style>
		<meta charset="utf-8">
		<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<title></title>
		<link rel="stylesheet" href="../css/styles.css" />
		<!-- <link rel="stylesheet" href="https://d10ajoocuyu32n.cloudfront.net/mobile/1.3.1/jquery.mobile-1.3.1.min.css"> -->
		<link rel="stylesheet" href="../css/jquery.mobile-1.3.1.min.css">
		<link rel="stylesheet" href="file:///android_asset/css/in.css">
		<!--<link type="text/css" href="http://dev.jtsage.com/cdn/datebox/latest/jqm-datebox.min.css" rel="stylesheet" /> -->
		<link type="text/css" href="../css/jqm-datebox.min.css" rel="stylesheet" /> 
		<!-- Extra Codiqa features -->
		<link rel="stylesheet" href="codiqa.ext.css">
		<!-- jQuery and jQuery Mobile -->
		<script src="https://d10ajoocuyu32n.cloudfront.net/jquery-1.9.1.min.js"></script>
		<script src="https://d10ajoocuyu32n.cloudfront.net/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>
		<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
		<!-- Extra Codiqa features -->
		<script src="https://d10ajoocuyu32n.cloudfront.net/codiqa.ext.js"></script>
		<script src="file:///android_asset/js/login.js"></script>
	</head>
<body>
			<div class="ui-header ui-bar-b" align="center" data-theme="b" data-role="header" role="banner">
				<p style="padding:10px;">
				<font size="3"> Settings </font></p>
				<img style="float:right;height:35px; margin:-37px 6px 0px 0px;width:50px;border-radius:10px;" src="../images/signout.png" onclick="signout()">
			</div>
           <!-- Header -->
			<div data-role="content" align="center">
				<form action="" method="" >
					<div style="text-align:left;color:DeepSkyBlue;">Profile</div>
					<div>
						<input name="select-fname" id="select-fname" placeholder="First Name" value="" type="text">
					</div>
					<div>
					   <input name="select-lname" id="select-lname" placeholder="Last Name" value="" type="text">
					</div>
					<div>
					   <input name="select-level" id="select-level" placeholder="USAC Level" value="" type="text">
					</div>
					<div>
					   <input name="select-mem" id="select-mem" placeholder="USAC Member" value="" type="text">
					</div>
					<div>
					   <input name="select-mail" id="select-mail"  placeholder="Email Address" value="" type="text" disabled >
					</div>
					<div>
					   <input name="select-address" id="select-address" placeholder="Address" value="" type="text">
					</div>
					<div>
					   <input name="select-CityStateZip" id="select-CityStateZip" placeholder="City/ST/Zip" value="" type="text">
					</div>
					<div>
					   <input name="select-Phone" id="select-Phone" placeholder="Phone" value="" type="text">
					</div>
					
					<div>
					  <input name="select-website" id="select-website" placeholder="Website" value="" type="text">
					</div>
					<div>
						<input name="select-agma" id="select-agma" placeholder="AGMA" value="" type="text">
					</div>
					<div>
						<input name="select-cwa" id="select-cwa" placeholder="CWA" value="" type="text">
					</div>
					<div>
						 <input name="select-usac" id="select-usac" placeholder="USAC" value="" type="text">
					</div>
					<div style="text-align:left;color:DeepSkyBlue;">Location Settings</div>
					<div style="margin:5px 0px 0px 0px">
						  <select id="select-HomeLocation" name="select-HomeLocation">
						  <option value="">Home Location</option>
						  </select>
					</div>
					<div>
						<a href="#" style="float:left;margin:0px 10px 0px 0px;"> <img src="../images/cancel.png" onclick="goBack()" width = "120" height="40"></a> 
						<a href="#" style="float:right;margin:0px 0px 0px 10px;"> <img src="../images/continue.png" onclick="UpdateAccount()" width = "120" height="40"></a>           
					</div>
					<div>
						<p style="font-size:75pt;">&nbsp;</p>
					</div>
			</form>
		</div>
</body>
	<div style="background-image: url(../images/topbar.png); bottom:0;text-align:center;margin:0px 0px 0px 0px;" id="sample" class="ui-footer-fixed " > 
		<div style="margin:8px 0px -7px 17px;">
			<img  style="margin:0px -1px 13px -17px;" class="folder" src="../images/folder.png"  height="40" width="40" onclick="folder()">
			<img  style="margin:0px -1px 13px 19px;" class="folder" src="../images/settings.png" height="40" width="40" onclick="settings()">
			<img  style="margin:0px -1px 13px 19px;" class="" src="../images/home.png" height="40" width="40" onclick="home()">
			<img  style="margin:0px -1px 13px 19px;" class="folder" src="../images/boulder.png" height="40" width="40" onclick="boulder()">
			<img  style="margin:0px -1px 13px 19px;" class="folder" src="../images/route.png" height="40" width="40" onclick="route()">
		</div>
	</div>					
</html>
	<script type="text/javascript" charset="utf-8" src="file:///android_asset/js/cordova.js"></script>
	<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
	<script>
	var userId1=window.localStorage["user"];var arr1 = [];var arr2 = [];var Location1='';var userid2='';var website='';
	var email=window.localStorage["username1"];var email1='';var userid='';var username='';var usacLevel='';var cwa='';
	var createDate='';var personId='';var address='';var modifyDate='';var agma='';var usac='';
	var firstname='';var lastname='';var usacMemberNo='';var city='';var phone='';var locationGroup='';var createDate3='';
	var token1=encodeURIComponent(window.localStorage["token"]);
	function signout(){
		window.location='index_login.html';
	}
	function folder(){
		<!-- window.location='http://forerunnerpro.com/?nr=0'; -->
		navigator.app.loadUrl('http://forerunnerpro.com/?nr=0', { openExternal:true });
	}
	function settings(){
		window.location='settings.html';
	}
	function home(){
		window.location='option.html';
	}
	function boulder(){
		window.location='boulder_option.html';
	}
	function route(){
		window.location='route_option.html';
	}
	function goBack(){
		window.location='option.html';
	}
	document.addEventListener("deviceready", updateRopeRoute, false);
	function updateRopeRoute(){
			//get userid corresponding values
			$.ajax({
				type: "GET",
				url: window.localStorage["url"]+'/getuserprofile/'+userId1+'?token='+token1,
				contentType: "application/json; charset=utf-8",
				dataType: "json",
				success: function(data){
					if(data.status == "SUCCESS"){
						$.each(data.data, function(key,value){
							arr1.push(key);
							if(key=="user"){
							$("#select-fname").val(value.firstName);
							$("#select-lname").val(value.lastName);
							$("#select-mail").val(value.email);
							
							window.localStorage["locationGroupId"]=value.locationGroupId;
							window.localStorage["createDate4"]=value.createDate;
							window.localStorage["username"]=value.username;
							}
							if(key=="person"){
							window.localStorage["personId"]=value.personId;
							window.localStorage["createDate2"]=value.createDate;
							window.localStorage["modifyDate1"]=value.modifyDate;
							
							$("#select-level").val(value.usacLevel);
							$("#select-mem").val(value.usacMemberNo);
							$("#select-address").val(value.address);
							$("#select-CityStateZip").val(value.city);
							$("#select-Phone").val(value.phone);
							$("#select-website").val(value.website);
							$("#select-agma").val(value.agma);
							$("#select-cwa").val(value.cwa);
							$("#select-usac").val(value.usac);
							}
						});
					}else if(data.errorCode=="100"){
						navigator.notification.alert("Please Sign In");
						window.location='index_login.html';
						return false;
					}
				}
			});
		//get the locations by using username
		if(window.localStorage["Homelocation"]==''){
		$.ajax({
				type: "GET",
				url: window.localStorage["url"]+'/locgroupsbyuser?userName='+email+'&token='+token1,
				contentType: "application/json; charset=utf-8",
				dataType: "json",
				success: function(data){
					if(data.status == "SUCCESS"){
						$.each(data.data, function(key,value){	
						Location1+="<option value="+value.locGroupId+">"+value.locGroupName+"</option>";
						});	
					$("#select-HomeLocation").append(Location1);			
					}
				}
		});	
		}else{
		$('#select-HomeLocation').parent().find('.ui-btn-text').text(window.localStorage["Homelocation"]);
		$.ajax({
				type: "GET",
				url: window.localStorage["url"]+'/locgroupsbyuser?userName='+email+'&token='+token1,
				contentType: "application/json; charset=utf-8",
				dataType: "json",
				success: function(data){
					if(data.status == "SUCCESS"){
						$.each(data.data, function(key,value){	
						Location1+="<option value="+value.locGroupId+">"+value.locGroupName+"</option>";
						});	
					$("#select-HomeLocation").append(Location1);			
					}
				}
		});	
		}
	}
function UpdateAccount(){
	var fname=$('#select-fname').val();
	var lname=$('#select-lname').val();
	var level=$('#select-level').val();
	var mem=$('#select-mem').val();
	var mail=$('#select-mail').val();
	var address=$('#select-address').val();
	var CityStateZip=$('#select-CityStateZip').val();
	var Phone1=$('#select-Phone').val();
	var HomeLocation1=$('#select-HomeLocation').val();
	var HomeLocation2=$('#select-HomeLocation').find(":selected").text();
	var website1=$('#select-website').val();
	var Agma=$('#select-agma').val();
	var Cwa=$('#select-cwa').val();
	var Usac=$('#select-usac').val();
	//user 
	userid="'userid':'"+userId1+"',";
	userid2="'userId':'"+userId1+"',";
	username="'username':'"+window.localStorage["username"]+"',";
	if(mail==""){
		email1="";
		}else{
		email1="'email':'"+mail+"',";}
	if(fname==""){
		navigator.notification.alert("First Name  is Mandatory");
		return false;
		}else{
		firstname="'firstName':'"+fname+"',";}
	if(lname==""){
		navigator.notification.alert("Last Name  is Mandatory");
		return false;
		}else{
		lastname="'lastName':'"+lname+"',";}
	if(HomeLocation1==""){
		window.localStorage["GroupId"] = HomeLocation1;
		locationGroup="'locationGroupId':'"+window.localStorage["locationGroupId"]+"',";
		}else{
		window.localStorage["Homelocation"]=HomeLocation2;
		locationGroup="'locationGroupId':'"+HomeLocation1+"',";}
	var totalValue="{"+userid+username+email1+firstname+lastname+locationGroup;
	totalValue = totalValue.replace(/'/g, '"');
	var strlen=totalValue.length;
	totalValue=totalValue.slice(0,strlen-1);
	totalValue+="}";
	if(arr1.length==1){
			window.localStorage["userIdp"]="";
			window.localStorage["personId"]="";
			window.localStorage["createDate2"]="";
			window.localStorage["modifyDate1"]="";
	}
	//person
	if(window.localStorage["personId"]==""){
		personId="";
		}else{
		personId="'personId':'"+window.localStorage["personId"]+"',";}
	if(level==""){
		usacLevel="";
		}else{
		usacLevel="'usacLevel':'"+level+"',";}
	if(mem==""){
		usacMemberNo="";
		}else{
		usacMemberNo="'usacMemberNo':'"+mem+"',";}
	if(address==""){
		address="";
		}else{
		address="'address':'"+address+"',";}
	if(CityStateZip==""){
		city="";
		}else{
		city="'city':'"+CityStateZip+"',";}
	if(Phone1==""){
		phone="";
		}else{
		phone="'phone':'"+Phone1+"',";}
	if(website1==""){
		website="";
		}else{
		website="'website':'"+website1+"',";}
	if(Agma==""){
		agma="";
		}else{
		agma="'agma':'"+Agma+"',";}
	if(Cwa==""){
		cwa="";
		}else{
		cwa="'cwa':'"+Cwa+"',";}
	if(Usac==""){
		usac="";
		}else{
		usac="'usac':'"+Usac+"',";}
	if(window.localStorage["createDate2"]==""){
		createDate3="";
		}else{
		var array2=window.localStorage["createDate2"].split('/');
		var Cdate=""+array2[0]+"-"+array2[1]+"-"+array2[2]+"";
		createDate3="'createDate':'"+Cdate+"',";}
	if(window.localStorage["modifyDate1"]==""){
		modifyDate="";
		}else{
		var array3=window.localStorage["modifyDate1"].split('/');
		var Mdate=""+array3[0]+"-"+array3[1]+"-"+array3[2]+"";
		modifyDate="'modifyDate':'"+Mdate+"',";}
	var personValue=level+mem+address+CityStateZip+Phone1+website1+Agma+Cwa+Usac;	
	if(personValue!=""){
		var totalValue1="{"+userid2+usacLevel+usacMemberNo+address+city+phone+website+agma+cwa+usac+createDate3+modifyDate;	
		totalValue1 = totalValue1.replace(/'/g, '"');
		var strlen=totalValue1.length;
		totalValue1=totalValue1.slice(0,strlen-1);
		totalValue1+="}";
			var finalValue="{"+"'user':";
			finalValue = finalValue.replace(/'/g, '"');
			finalValue+=totalValue+",";
			var personValue="'person':";
			personValue = personValue.replace(/'/g, '"');
			finalValue+=personValue+totalValue1+"}";
			window.localStorage["finalValue"]=finalValue;
			//navigator.notification.alert(window.localStorage["finalValue"]);
		//update the values		
			$.ajax({
				type: "POST",
				url: window.localStorage["url"]+'/updateprofile?token='+token1,
				contentType: "application/json; charset=utf-8",
				data:window.localStorage["finalValue"],
				success: function(data){
					if(data.status == "SUCCESS"){
						navigator.notification.alert("The Route Database Has Been Updated");
						window.location='option.html';					
						}
					else if(data.errorCode=="100"){
						navigator.notification.alert("Please Sign In");
						window.location='index_login.html';
						return false;
						}
					else{
						navigator.notification.alert(data.errorMessage);
						}				
					}
			});	
		}else{
			var finalValue1="{"+"'user':";
			finalValue1 = finalValue1.replace(/'/g, '"');
			var personValue1="'person':";
			personValue1+="{";
			personValue1+="'userId':'"+userId1+"'}";
			personValue1 = personValue1.replace(/'/g, '"');
			finalValue1+=totalValue+",";
			finalValue1+=personValue1+"}";
			window.localStorage["finalValue1"]=finalValue1;
			//navigator.notification.alert(window.localStorage["finalValue1"]);
		//update the values		
			$.ajax({
			type: "POST",
			url: window.localStorage["url"]+'/updateprofile?token='+token1,
			contentType: "application/json; charset=utf-8",
			data:window.localStorage["finalValue1"],
			success: function(data){
				if(data.status == "SUCCESS"){
					navigator.notification.alert("The Route Database Has Been Updated");
					window.location='option.html';					
					}
				else if(data.errorCode=="100"){
						navigator.notification.alert("Please Sign In");
						window.location='index_login.html';
						return false;
					}	
				else{
					navigator.notification.alert(data.errorMessage);
					}				
				}
			});	
		}	
}	
</script>